package com.bwf.file;

import java.io.FileWriter;
import java.io.IOException;

/**
 * @Version v1.0
 * @Description: TODO
 * @Author ZHANZHONGXIN
 * @Date 2022/5/16 16:01
 * @Copyright 博为峰
 */
public class FileWriterDemo {
    public static void main(String[] args) {
//        m1();
//        m2();
        //try-with-resource
        //把需要关闭的资源放在try后面的(), AutoCloseable, Colseable
        try (FileWriter fileWriter = new FileWriter("fw01.txt", true);) {
            fileWriter.write("abc");
        } catch (IOException e) {

        }

    }

    private static void m2() {
        FileWriter fileWriter = null;
        try {
            System.out.println(1 / 0);
            fileWriter = new FileWriter("fw01.txt", true);
//            System.out.println(1 / 0);
            fileWriter.write("666");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileWriter != null) {
                try {
                    System.out.println("close方法执行");
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static void m1() throws IOException {
        FileWriter fileWriter = new FileWriter("fw01.txt", true);
        fileWriter.write("abc");
        fileWriter.close();
    }
}
